﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ValiVIN.Manufacturer
{
    internal class Lotus : IVINManufacturer
    {
        //Example VIN: SCCPC11185HL31449

        #region Singleton

        static readonly Lotus _instance = new Lotus();

        public static Lotus Instance
        {
            get { return _instance; }
        }

        #endregion

        private void GetSerialNumber(VINumber vin)
        {
            //14 - 17
            string serialNum = vin.VIN.Substring(13, 4);
            vin.SerialNumber = serialNum;
        }

        private void GetBody(VINumber vin)
        {
            //12 A = LHD USA, B = LHD Canadian, C = RHD other, D = LHD Other, F = Approved UK SVA RHD, 
            //G = Approved UK SVA LHD, H = LHD MotorSport, J = RHD MotorSPort, K = Central MotorSport, 
            //L = LHD USA AC, M = LHD Canadian AC, M = RHD Other AC, P = LHD Other AC, R=UK SVA RHD AC, S UK SVA LHD, AC

            string key = "Body";
            string body = string.Empty;
            string b = vin.VIN.Substring(12, 1);

            switch (b)
            {
                case "A":
                    body = "LHD USA";
                    break;
                case "B":
                    body = "LHD Canadian";
                    break;
                case "C":
                    body = "RHD Other";
                    break;
                case "D":
                    body = "LHD Other";
                    break;
                case "F":
                    body = "Approved UK SVA RHD";
                    break;
                case "G":
                    body = "Approved UK SVA LHD";
                    break;
                case "H":
                    body = "LHD MotorSport";
                    break;
                case "J":
                    body = "RHD MotorSport";
                    break;
                case "K":
                    body = "Central MotorSport";
                    break;
                case "L":
                    body = "LHD USA a.c.";
                    break;
                case "M":
                    body = "LHD Canadian a.c.";
                    break;
                case "P":
                    body = "LHD Other a.c.";
                    break;
                case "R":
                    body = "UK SVA RHD a.c.";
                    break;
                case "S":
                    body = "UK SVA LHD a.c.";
                    break;

            }

            if (!string.IsNullOrEmpty(body))
            {
                vin.ManufacturerSpecifics[key] = body;
            }
        }

        private void GetModel(VINumber vin)
        {
            //13 3=Elise,8=Exige
            string key = "Model";
            string model = string.Empty;
            string m = vin.VIN.Substring(12, 1);

            switch (m)
            {
                case "3":
                    model = "Elise";
                    break;
                case "8":
                    model = "Exige";
                    break;
            }

            if (!string.IsNullOrEmpty(model))
            {
                vin.ManufacturerSpecifics[key] = model;
            }
        }

        private void GetVehicleType(VINumber vin)
        {
            //6 111=Elise, 123= Europa
            string key = "Vehicle Type";
            string vehicle = string.Empty;
            string v = vin.VIN.Substring(5, 3);

            if (v == "111")
            {
                vehicle = "Elise";
            }
            else if (v == "123")
            {
                vehicle = "Europa";
            }

            if (!string.IsNullOrEmpty(vehicle))
            {
                vin.ManufacturerSpecifics[key] = vehicle;
            }
        }

        private void GetRestraint(VINumber vin)
        {
            //5 A = Active Belts, B = SIR driver and active belts, C=SIR Driver/passenger and active belts,P = Passive Belts
            string key = "Restraint";
            string restraint = string.Empty;
            string r = vin.VIN.Substring(4, 1);

            switch (r)
            {
                case "A":
                    restraint = "Active Belts";
                    break;
                case "B":
                    restraint = "SIR Driver and Active Belts";
                    break;
                case "C":
                    restraint = "SIR Driver/Passenger and Active Belts";
                    break;
                case "P":
                    restraint = "Passive Belts";
                    break;
            }

            if (!string.IsNullOrEmpty(restraint))
            {
                vin.ManufacturerSpecifics[key] = restraint;
            }
        }

        private void GetEngineType(VINumber vin)
        {
            //4 P=2ZZ NA, S=Europa, V=2ZZ S/C 220, W=2ZZ SC 240, Y=1ZZ, Z = sc non c/c, L = motorsport
            string engine = string.Empty;
            string e = vin.VIN.Substring(3, 1);

            switch (e)
            {
                case "P":
                    engine = "2ZZ";
                    break;
                case "S":
                    engine = "Europa";
                    break;
                case "V":
                    engine = "2ZZ S/C 220";
                    break;
                case "W":
                    engine = "2ZZ S/C 240";
                    break;
                case "Y":
                    engine = "1ZZ";
                    break;
                case "Z":
                    engine = "S/C non C/C";
                    break;
                case "L":
                    engine = "Motorsport";
                    break;
            }

            if (!string.IsNullOrEmpty(engine))
            {
                vin.EngineType = engine;
            }
        }

        private void GetPlant(VINumber vin)
        {
            //11 H=Hethel, B=Shah Alam
            string p = vin.VIN.Substring(10, 1);
            if (p == "H")
            {
                vin.Plant = "Hethel";
            }
            else if (p == "B")
            {
                vin.Plant = "Shah Alam";
            }
        }

        #region IVINManufacturer Members

        public bool Decode(VINumber vin)
        {
            GetPlant(vin);
            GetEngineType(vin);
            GetRestraint(vin);
            GetVehicleType(vin);
            GetModel(vin);
            GetBody(vin);
            GetSerialNumber(vin);
            return true;
        }

        public string ManufacturerName(string vin)
        {
            return "Lotus Cars";
        }

        #endregion
    }
}
